package com.tanjie.blackbean.system.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import com.tanjie.blackbean.system.admin.model.org.OrgDTO;
import com.tanjie.blackbean.system.admin.model.org.OrgPO;
import com.tanjie.blackbean.system.admin.model.org.OrgQuery;
import com.tanjie.blackbean.system.admin.model.org.OrgVO;
import com.tanjie.blackbean.system.admin.service.IOrgService;
import com.tanjie.blackbean.web.annotation.BlackBeanController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 组织机构
 *
 * @author tanjie
 * @since 2022-04-19
 */
@Tag(name = "组织机构")
@BlackBeanController
@RequestMapping("org")
public class OrgController {

    @Resource
    IOrgService sysOrgService;

    @Operation(summary = "新增")
    @PostMapping("create")
    public OrgPO createSysOrgHandler(@RequestBody @Validated OrgDTO dto) {
        return sysOrgService.create(dto);
    }

    @Operation(summary = "更新")
    @PostMapping("update")
    public void updateSysOrgHandler(@RequestBody @Validated OrgDTO dto) {
        sysOrgService.update(dto);
    }

    @Operation(summary = "删除")
    @GetMapping("delete")
    public void deleteSysOrgHandler(String id) {
        sysOrgService.delete(id);
    }

    @Operation(summary = "获取详情")
    @GetMapping("detail")
    public OrgVO getSysOrgDetailHandler(String id) {
        return sysOrgService.detail(id);
    }

    @Operation(summary = "列表查询")
    @PostMapping("list")
    public List<OrgVO> querySysOrgListHandler(@RequestBody(required = false) OrgQuery dto) {
        return sysOrgService.queryList(dto);
    }

    @Operation(summary = "查询组织机构树")
    @PostMapping("queryTree")
    public List<Tree<String>> queryTreeHandler() {
        return sysOrgService.queryTree();
    }

    @Operation(summary = "子树异步（延迟）加载", description = "仅获取子级")
    @GetMapping(path = {"lazyTree", "getChildren"})
    public List<OrgVO> lazyTreeHandler(@RequestParam("parentId") String parentId) {
        return sysOrgService.lazyTree(parentId);
    }

}
